$(function() {
	$("#history_list").on("click", ".query-info", function() {
		var data = dataTablesReload.row($(this).parents('tr')).data();
		$.ajax({
			type: "POST",
			url: "http://127.0.0.1:8080/history/getHistroyInfo",
			cache: false,
			xhrFields: {
				withCredentials: true
			},
			data: {
				"id": data.id
			},
			dataType: "json",
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					$("#originalText").text(data.data.originalText);
					$("#cipherText").text(data.data.cipherText);
					$("#password").text(data.data.password);
					$("#operationType").text(data.data.operationType);
					$("#type").text(data.data.type);
					$("#addRowModal").modal('show');
				} else {
					notifyMsg(data.msg);
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				notifyMsg('获取分页数据失败');
			}
		});
	})

	//列表显示
	var dataTablesReload = $('#basic-datatables').DataTable({
		language: {
			"sLengthMenu": "显示 _MENU_ 项结果",
			"sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
			"sSearch": "搜索:",
			"oPaginate": {
				"sPrevious": "上页",
				"sNext": "下页"
			}
		},
		pageLength: 10,
		pagingType: "simple",
		bFilter: false,
		responsive: false,
		bAutoWidth: true,
		bLengthChange: true,
		serverSide: true,
		ordering: false,
		ajax: function(data, callback, settings) {
			var pagesize = data.length;
			var page = (data.start) / data.length + 1;
			$.ajax({
				type: "POST",
				url: "http://127.0.0.1:8080/history/findHistoryListByPage",
				cache: false,
				xhrFields: {
					withCredentials: true
				},
				data: {
					"pageNumber": page,
					"pageSize": pagesize
				},
				dataType: "json",
				success: function(data) {
					var code = data.code;
					if (code == 'success') {
						var returnData = {};
						returnData.recordsTotal = data.data.count;
						returnData.recordsFiltered = data.data.count;
						returnData.data = data.data.historyList;
						callback(returnData);
					} else {
						notifyMsg(data.msg);
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					notifyMsg('获取分页数据失败');
				}
			});
		},
		columns: [{
				"data": "originalText",
				"defaultContent": "<i></i>"
			},
			{
				"data": "cipherText",
				"defaultContent": "<i></i>"
			},
			{
				"data": "password",
				"defaultContent": "<i></i>"
			},
			{
				"data": "operationType",
				"defaultContent": "<i></i>"
			},
			{
				"data": "type",
				"defaultContent": "<i></i>"
			},
			{
				"data": "",
				"defaultContent": '<div class="form-button-action"><button type="button" data-toggle="tooltip"' +
					' title="" class="btn btn-link btn-primary btn-lg query-info" data-original-title="查看详情">' +
					'<i class="fas fa-info"></i></div>'
			}
		]
	});
})
